//
//  ADModel.m
//  RainbowFM
//
//  Created by chenli on 14/12/28.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "ADModel.h"
#import "AdAPI.h"
#import "LoginModel.h"
#import "UserModel.h"

@implementation ADModel

+(instancetype)sharedModel
{
    static ADModel *adModel = nil;
    static dispatch_once_t once;
    
    dispatch_once(&once, ^{
        adModel = [[ADModel alloc] init];
    });
    
    return adModel;
}

-(void)fetchADListWithChannel:(NSString *)channel code:(NSString *)code_ result:(void (^)(BOOL, NSArray *))res{
    AdAPI *api = [[AdAPI alloc] init];
    [api fetchAdListWithChannel:channel code:code_ userID:[LoginModel sharedModel].userID == nil ? @"" : [LoginModel sharedModel].userID result:^(NSArray *array, NSString *msg) {
        if (array != nil) {
            NSMutableArray *listArray = [NSMutableArray array];
            for (NSDictionary *dict in array) {
                [listArray addObject:[[ADListItem alloc] initWithAttributes:dict]];
            }
            
            res(YES,listArray);
        }else{
            res(NO,nil);
        }
       
    }];
}


-(void)adStatisticsWithBackParam:(NSString *)backParam result:(void (^)(BOOL, NSString *))res{
    AdAPI *api = [[AdAPI alloc] init];
    [api adStatisticsWithBackParam:backParam
                            userID:[LoginModel sharedModel].userID == nil ? @"" : [LoginModel sharedModel].userID
                          phoneNum:[UserModel sharedModel].userInfo.userTel == nil ? @"" : [UserModel sharedModel].userInfo.userTel
                            result:^(BOOL success, NSString *msg) {
                                if (success) {
                                    NSLog(@"广告统计");
                                }else{
                                    NSLog(@"广告统计错误");
                                }
                            }];
}


@end


@implementation ADListItem

-(instancetype)initWithAttributes:(NSDictionary *)attributes{
    self = [super init];
    if (!self) {
        return nil;
    }
    
    _backParam     = attributes[@"backParam"];
    _imgBig        = attributes[@"imgBig"];
    _imgSmall      = attributes[@"imgSmall"];
    _interval      = attributes[@"interval"];
    _name          = attributes[@"name"];
    _offlineTime   = attributes[@"offlineTime"];
    _onlineTime    = attributes[@"onlineTime"];
    _relationId    = attributes[@"relationId"];
    _relationTitle = attributes[@"relationTitle"];
    _text          = attributes[@"text"];
    _time          = attributes[@"time"];
    _type          = attributes[@"type"];
    _url           = attributes[@"url"];
    _video         = attributes[@"video"];
    
    
    return self;
}

@end